package com.hf.wujian;

import com.hf.wujian.model.imp.BSHGameHall;
import com.hf.wujian.netty.BSHServerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.Map;


/**
 * 服务器执行类
 *
 * @author hf
 * @date 2018/11/23
 * @version 1.0
 */
public class DispatchServer {

    /**
     * 监听端口
     */
    private int portNumber;
    /**
     * 调度器实现类map
     */
    private Map<Integer,Class> dispatchs;

    public DispatchServer(int portNumber,Map<Integer,Class> dispatchs){
        this.portNumber = portNumber;
        this.dispatchs = dispatchs;
    }

    public void start(){
        BSHGameHall gameHall = new BSHGameHall();

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup);
            serverBootstrap.channel(NioServerSocketChannel.class);
            serverBootstrap.childHandler(new BSHServerInitializer(gameHall,dispatchs));

            System.out.println("服务器启动——————");
            ChannelFuture channelFuture = serverBootstrap.bind(portNumber).sync();

            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
